﻿using System.Collections.Generic;
using System.Threading.Tasks;

namespace SqlSugarCore.Dal.BaseRealize
{
    /// <summary>
    /// 写入相关基本实现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public partial class BaseDal<T> where T : class, new()
    {
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(T entity)
        {
            var insertNum = this._sqlSugarClient.Insertable<T>(entity).ExecuteCommand();
            return insertNum;
        }

        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(T entity)
        {
            var insertNum = await this._sqlSugarClient.Insertable<T>(entity).ExecuteCommandAsync();
            return insertNum;
        }


        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities"></param>
        /// <returns>返回受影响的行数</returns>
        public int Insert(List<T> entities)
        {
            var insertNum = this._sqlSugarClient.Insertable<T>(entities).ExecuteCommand();
            return insertNum;
        }

        /// <summary>
        /// 批量新增(异步)
        /// </summary>
        /// <param name="entities">表实体</param>
        /// <returns>返回受影响的行数</returns>
        public Task<int> InsertAsync(List<T> entities)
        {
            var insertNum = this._sqlSugarClient.Insertable<T>(entities).ExecuteCommandAsync();
            return insertNum;
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回自增列</returns>
        public int InsertReturnIdentity(T entity)
        {
            var insertNum = this._sqlSugarClient.Insertable<T>(entity).ExecuteReturnIdentity();
            return insertNum;
        }

        /// <summary>
        /// 新增(异步)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回自增列</returns>
        public async Task<int> InsertReturnIdentityAsync(T entity)
        {
            var insertNum = await this._sqlSugarClient.Insertable<T>(entity).ExecuteReturnIdentityAsync();
            return insertNum;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回新增的实体</returns>
        public T InsertReturnEntity(T entity)
        {
            var insertEntity = this._sqlSugarClient.Insertable<T>(entity).ExecuteReturnEntity();
            return insertEntity;
        }

        /// <summary>
        /// 新增 (异步)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回新增的实体</returns>
        public async Task<T> InsertReturnEntityAsync(T entity)
        {
            var insertEntity = await this._sqlSugarClient.Insertable<T>(entity).ExecuteReturnEntityAsync();
            return insertEntity;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回新增的实体并带出自增列值</returns>
        public bool InsertRerunIdentityIntoEntity(T entity)
        {
            var insertBool = this._sqlSugarClient.Insertable<T>(entity).ExecuteCommandIdentityIntoEntity();
            return insertBool;
        }

        /// <summary>
        /// 新增 (异步)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>返回新增的实体并带出自增列值</returns>
        public async Task<bool> InsertRerunIdentityIntoEntityAsync(T entity)
        {
            var insertBool = await this._sqlSugarClient.Insertable<T>(entity).ExecuteCommandIdentityIntoEntityAsync();
            return insertBool;
        }
    }
}